package com.tang.java8.Stream;

import java.util.Arrays;
import java.util.List;


/**
 * @author tlm
 * @version 1.0
 * @date 2020/3/29 16:18
 * Stream 对User对象进行综合操作
 * 条件如下:
 * 1、ID 必须是奇数
 * 2、年龄必须大于30岁
 * 3、用户名转为大写字母
 * 4、只输出一个用户
 */
public class StreamTest2 {
    public static void main(String[] args) {
        User user1 = new User(1, "ZhangSan", 20);
        User user2 = new User(2, "LiSi", 18);
        User user3 = new User(3, "WangWu", 19);
        User user4 = new User(4, "ZhaoLiu", 30);
        User user5 = new User(5, "JackMa", 45);
        //把User对象放到List中
        List<User> userList = Arrays.asList(user1, user2, user3, user4, user5);
        //使用Stream流计算
        userList.stream()
                //年龄为奇数
                .filter(user -> {
                    return user.getAge() % 2 == 1;
                })
                //年龄大于30
                .filter(user -> {
                    return user.getAge() > 30;
                })
                //用户名转为大写字母
                .map(user -> {
                    return user.getName().toUpperCase();
                })
                //只输出一个用户
                .limit(1).forEach(System.out::println);
/*
输出结果：
JACKMA
 */


    }
}

class User {
    private Integer id;
    private String name;
    private Integer age;

    public User(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
